package com.wbs.shop.nuby.view.base;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wbs.shop.nuby.utils.Logger;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.BehaviorSubject;

/**
 * Created by vicky on 2016/11/7.
 */
public abstract class BaseFragment<VM extends BaseViewModel> extends Fragment implements LifecycleProvider {
    protected final BehaviorSubject<LifecycleEvent> lifecycleSubject = BehaviorSubject.create();
    private CompositeDisposable mCompositeDisposable;
    private VM mModel;
    private View mView;
    private Unbinder unbinder;

    public VM getViewModel() {
        return mModel;
    }

    public void setViewModel(Class<VM> tClass) {
        mModel = addViewModelClass(tClass);
    }

    private VM addViewModelClass(Class<VM> tClass) {
        try {
            if (tClass != null) {
                VM viewModel = tClass.newInstance();
                viewModel.onBind(this);
                return viewModel;
            }
        } catch (Exception e) {
            Logger.e("创建model失败!!" + tClass.getSimpleName());
        }
        return null;
    }

    public BehaviorSubject<LifecycleEvent> getLifeSubject() {
        return lifecycleSubject;
    }

    public void add(Disposable d) {
        if (mCompositeDisposable != null)
            mCompositeDisposable.add(d);
    }

    @Override
    public void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCompositeDisposable = new CompositeDisposable();
        lifecycleSubject.onNext(LifecycleEvent.CREATE);
    }

    public boolean hasExtras() {
        return getArguments() != null;
    }

    protected abstract int tellMeLayout();

    @Nullable
    @Override
    public View getView() {
        return mView;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        try {
            lifecycleSubject.onNext(LifecycleEvent.CREATE_VIEW);
            if (tellMeLayout() != 0) {
                mView = inflater.inflate(tellMeLayout(), null);
            } else {
                mView = super.onCreateView(inflater, container, savedInstanceState);
            }
            unbinder = ButterKnife.bind(this, mView);
            init(mView, savedInstanceState);
        } catch (Exception e) {
            Logger.e(e);
        }
        return mView;
    }

    public void init(View rootView, Bundle savedInstanceState) {
    }

    public <T extends View> T findViewById(@IdRes int id) {
        return mView.findViewById(id);
    }

    @Override
    public void onStart() {
        super.onStart();
        lifecycleSubject.onNext(LifecycleEvent.START);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mModel != null) {
            mModel.onResume();
        }
        lifecycleSubject.onNext(LifecycleEvent.RESUME);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mModel != null) {
            mModel.onPause();
        }
        lifecycleSubject.onNext(LifecycleEvent.PAUSE);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mModel != null) {
            mModel.onStop();
        }
        lifecycleSubject.onNext(LifecycleEvent.STOP);
    }

    @Override
    public void onDestroy() {
        lifecycleSubject.onNext(LifecycleEvent.DESTROY);
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        lifecycleSubject.onNext(LifecycleEvent.DETACH);
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mModel != null) {
            mModel.onDestroy();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }
        lifecycleSubject.onNext(LifecycleEvent.DESTROY_VIEW);
    }

    @Override
    public void onAttach(android.app.Activity activity) {
        super.onAttach(activity);
        lifecycleSubject.onNext(LifecycleEvent.ATTACH);
    }

    public void onWindowFocusChanged(boolean hasFocus) {

    }

    final public void goActicity(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    final public void goActicity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }


    final public void goActicityFinish(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
        getActivity().finish();
    }

    final public void goActicityForResult(Class<?> clazz, int requestCode) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivityForResult(intent, requestCode);
    }


    final public void goActicityForResult(Class<?> clazz, int requestCode, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }
}
